package com.Modular.pdf_Util.examplesPDF.CheLiang.CheLiangJinChangChuChang;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;

import com.Modular.MinioPlugin.MinIoOperations;
import com.Modular.pdf_Util.PDF_utils;
import com._Config.Model.errorLogModel;
import com.jfinal.core.Controller;
import com.jfinal.log.Log4jLog;
import org.apache.commons.lang3.time.DateFormatUtils;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 车辆进场出场登记
 */
public class CheLiangJinChangChuChangDengJiService extends Controller {
    static Log4jLog logger= Log4jLog.getLog(CheLiangJinChangChuChangDengJiService.class);

    public static final CheLiangJinChangChuChangDengJiService service = new CheLiangJinChangChuChangDengJiService();

    public String buildPdf(String danWeiDaiMa,String startTime,String endTime) {

        try {

            if(startTime==null&&endTime==null) {
               startTime = DateUtil.format(DateUtil.lastWeek(), "yyyy-MM-dd") + " 00:00:00";
               endTime = DateUtil.format(new Date(), "yyyy-MM-dd")+" 23:59:59";
            }

            List<errorLogModel> dataList
                    = errorLogModel.dao.find("SELECT * FROM errorLogModel " +
                            " WHERE sys_xitongzhuangtai=0 AND SYS_DanWeiDaiMa=? and DengJiShiJian >=? AND DengJiShiJian <=? "
                    ,danWeiDaiMa,startTime,endTime);


            List<Map<String , Object>> mapList=new ArrayList<>();
            String qiYeMingCheng = "",tingChaChangMingCheng="";
            for (int i = 0; i < dataList.size(); i++) {
                mapList.add(jinChang(dataList.get(i),i+1));
                qiYeMingCheng= (String) mapList.get(0).get("QiYeMingCheng");
                tingChaChangMingCheng= (String) mapList.get(0).get("TingChaChangMingCheng");
            }
            String time=DateFormatUtils.format(new Date(),"yyyy-MM-dd");
            Map<String,String> titleMap=new HashMap<>();
            titleMap.put("QiYeMingCheng",qiYeMingCheng);
            titleMap.put("TingChaChangMingCheng",tingChaChangMingCheng);
            titleMap.put("Time",time);

            String dir=System.getProperty("user.dir");
            String fileName=String.format("%s%s.pdf","车辆进场出场登记表", DateUtil.format(new Date(),"yyyyMMddHHmmss"));
            String outPath = String.format("%s%s%s",dir,"/webapp/WEB-INF/template/temp/",fileName);

            String pdfName = String.format("%s.pdf","车辆进场出场登记表-"+time);
             //生成本地pdf 一表单+一表格
            PDF_utils.buildPdf("车辆进场出场登记表-模板.docx",outPath,titleMap,mapList);

            logger.debug("fileName:%s,output:%s",fileName,outPath);
            MinIoOperations.fileUpload("asiatrip", pdfName,outPath);
            String geturl = MinIoOperations.fileGeturlGet("asiatrip", pdfName);

            String SYS_DanWeiMingCheng=null;
            if(dataList.size()>0){
                SYS_DanWeiMingCheng=dataList.get(0).getStr("SYS_DanWeiMingCheng");
            }
            //关连电子档案
            dangAnWenJian("车辆进场出场登记表",pdfName,geturl,null,SYS_DanWeiMingCheng,danWeiDaiMa);
            return geturl;
        }catch(Exception e){
            throw new RuntimeException(e);
        }
    }

    public Map<String, Object> jinChang(errorLogModel model,int num){
        Map<String, Object> data = new HashMap<>();
        data.put("TingChaChangMingCheng",model.getStr("TingChaChangMingCheng"));
        data.put("QiYeMingCheng",model.getStr("SYS_DanWeiMingCheng"));
        data.put("ChePaiHaoMa",model.getStr("ChePaiHaoMa"));
        data.put("ChePaiYanSe", model.getStr("ChePaiYanSe"));
        data.put("JiaShiYuan", model.getStr("JiaShiYuan")==null?"":model.getStr("JiaShiYuan"));
        data.put("DengJiRiQi", DateUtil.parse(model.getStr("DengJiRiQi")).toString("yyyy-MM-dd"));
        data.put("DengJiShiJian", DateUtil.parse(model.getStr("DengJiShiJian")).toString("hh:MM:ss"));
        data.put("DengJiLeiXing",model.getStr("DengJiLeiXing"));
        data.put("CheLiangLeiXing",model.getStr("CheLiangLeiXing")==null?"":model.getStr("CheLiangLeiXing"));
        data.put("DingSunQingKuang",model.getStr("DingSunQingKuang")==null?"":model.getStr("DingSunQingKuang"));
        data.put("DengJiXinXiBeiZhu",model.getStr("DengJiXinXiBeiZhu")==null?"":model.getStr("DengJiXinXiBeiZhu"));
        data.put("ZhongDian",model.getStr("ZhongDian"));
        data.put("TianQiZhuangKuang",model.getStr("TianQiZhuangKuang"));
        data.put("ZhiBanRenYuan",model.getStr("ZhiBanRenYuan"));
        data.put("ZhuangTai",model.getStr("ZhuangTai")==null?"":model.getStr("ZhuangTai"));
        data.put("num",num);
        return data;
    }

    //关连电子档案
    public void dangAnWenJian(String muLuName,String fileName,String filePath,String SYS_DanWeiDianNaoBianHao ,
                              String SYS_DanWeiMingCheng ,String SYS_DanWeiDaiMa){
        //创建文件记录（电子档案）
    	errorLogModel fileModel=new errorLogModel();
        fileModel.set("Id",  IdUtil.randomUUID());
        fileModel.set("ZhuId","13b8805f-2dd9-46dd-ac10-e4d409ac1116");//车辆安全检查
        fileModel.set("WenJianZiLei",muLuName);
        fileModel.set("WenJianLeiBie","隐患排查与治理");
        fileModel.set("WenJianMingCheng",fileName);
        fileModel.set("WenJianLaiYuan","系统生成");
        fileModel.set("WenJianWeiZhi",filePath);
        fileModel.set("SYS_DanWeiDianNaoBianHao",SYS_DanWeiDianNaoBianHao);
        fileModel.set("SYS_DanWeiMingCheng",SYS_DanWeiMingCheng);
        fileModel.set("SYS_DanWeiDaiMa",SYS_DanWeiDaiMa);
        fileModel.set("GengXinShiJian",new Date());
        fileModel.set("BianHao",1);
        fileModel.save();
    }

}
